package freespiritsocial.io.user.ui.fragment.mine

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelStoreOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.scwang.smart.refresh.layout.util.SmartUtil
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.databinding.FrgMinePostListBinding
import freespiritsocial.io.user.decoration.SpaceItemDecoration
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ui.adapter.PostAdapter
import freespiritsocial.io.user.ui.fragment.post.PostDetailFrg
import freespiritsocial.io.user.viewmodel.MineViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

class MinePostListFrg: BaseFragment<MineViewModel,FrgMinePostListBinding>() {

    var type: Int = 0
    private val isUser: Boolean get() = mViewModel.isUser.get()

    companion object {
        fun create(type: Int,viewModelOwner: ViewModelStoreOwner) = MinePostListFrg().apply {
            this.type = type
            this.viewModelOwner = viewModelOwner
        }
    }

    override fun layoutId(): Int = R.layout.frg_mine_post_list

    private val postsListAdapter by lazy(LazyThreadSafetyMode.NONE) {
        PostAdapter(userSelf = isUser).apply {
            setOnItemClickListener { _,_,position ->
                gotoPostDetail(data[position].invitationId)
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.recyclerPost.init(
            postsListAdapter,
            GridLayoutManager(requireContext(),2),
            emptyImage = R.mipmap.empty_posts_list,
            emptyText = "There are currently no posts.",
            isScroll = false
        ).addItemDecoration(SpaceItemDecoration(SmartUtil.dp2px(if (isUser) 0f else 17.5f),SmartUtil.dp2px(5f)))
    }

    override fun createObserver() {
        super.createObserver()
        eventViewModel.delPostEvent.observe(this) { id ->
            removeListDataById(postsListAdapter.data,id)
            postsListAdapter.notifyDataSetChanged()
        }
    }

    override fun lazyLoadData() {
        getData(type,true)
    }

    //获取列表数据
    private fun getData(type: Int,refresh: Boolean = false) {
        mViewModel.getList(type,refresh,if (refresh) null else postsListAdapter.data.lastOrNull()?.invitationId) { result ->
            if (refresh)
                postsListAdapter.data.clear()
            if (type == MineFrg.TYPE_SAVED) {
                result.data.forEach {
                    it.saveFlag = true
                }
            }
            postsListAdapter.addData(result.data)
            updateLayoutSize()
        }
    }

    private fun removeListDataById(list: MutableList<PostPojo>,id: Long?) {
        list.forEach {
            if (it.invitationId == id) {
                list.remove(it)
                return
            }
        }
    }

    fun onShow() {
        updateLayoutSize()
    }

    private fun updateLayoutSize() {
        mDatabind.recyclerPost.post {
            mViewModel.changeViewHeightEvent.postValue(0)
        }
    }

    fun onRefresh() {
        getData(type,true)
    }

    fun onLoadMore() {
        getData(type)
    }


    private fun gotoPostDetail(invitationId: Long) {
        mViewModel.getPost(invitationId) {
            nav().navigateAction(
                R.id.postDetailFrg,
                bundle = bundleOf(PostDetailFrg.KEY_DATA to it)
            )
        }
    }
}